package com.yl.shiro;

import com.yl.model.User;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

/**
 * 功能或描述：shiro相关工具
 * package com.up360.passport.security.shiro
 *
 * @author DR.YangLong
 *         date 14-10-14
 *         time 下午2:14
 *         email 410357434@163.com
 * @version 1.0
 * @since 1.0
 * module 修正:            日期：
 */
public class ShiroUtil {
    /**
     * 从凭证组中获取当前用户，仅获取第一个realm中的第一个对象
     *
     * @param subject shiro subject
     * @return User
     */
    public static User getUserByPrincipals(Subject subject) {
        String realmName = subject.getPrincipals().getRealmNames().iterator().next();
        User user = (User) subject.getPrincipals().fromRealm(realmName).iterator().next();
        return user;
    }

    /**
     * 使用principals和realm获取放到principalCollection中的User对象
     *
     * @param principals 凭证组
     * @param realmName  realmName
     * @return User
     */
    public static User getUserByPrincipalsRealmName(PrincipalCollection principals, String realmName) {
        User user = (User) principals.fromRealm(realmName).iterator().next();
        return user;
    }
}
